using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ruminate.GUI.Framework;
using Ruminate.Utils;

namespace Ruminate.GUI.Content {

    public class Button : Element {

        /*####################################################################*/
        /*                               Variables                            */
        /*####################################################################*/

        /* ## Local Variables ## */
        private SlidingDoorRenderer _slidingDoorRenderer;
        private TextRenderer _textRenderer;
        private Texture2D _text, _default, _hover, _pressed;

        /* ## Exposed Variables ## */
        public string Label { get; set; }
        public ElementEvent ClickEvent { get; set; }
        

        /*####################################################################*/
        /*                        Node Initialization                         */
        /*####################################################################*/

        public Button(int x, int y, string label, ElementEvent buttonEvent) 
            : base(new Offset(x, y, 0, 0)) {

            ClickEvent = buttonEvent;            
            Label = label;
        }

        /*####################################################################*/
        /*                       Widget Initialization                        */
        /*####################################################################*/

        protected internal override void Init() {

            _slidingDoorRenderer = (SlidingDoorRenderer)InstanceData.DefaultRenderers["Button"];
            _textRenderer = (TextRenderer)InstanceData.DefaultRenderers["TextRenderer"];

            var size = _textRenderer.Font.MeasureString(Label);
            LocationComponent.Size = new Vector2(
                size.X + (2 * _slidingDoorRenderer.EndWidth),
                _slidingDoorRenderer.Height);
        }

        protected internal override void Render() {

            var size = LocationComponent.Size.ToPoint();

            _default = _slidingDoorRenderer.Render(size, 0);
            _hover = _slidingDoorRenderer.Render(size, 1);
            _pressed = _slidingDoorRenderer.Render(size, 2);

            _text = _textRenderer.Render(size, Label, 
                TextHorizontal.CenterAligned, 
                TextVertical.CenterAligned);
        }

        protected internal override Rectangle ChildRenderArea() {

            return LocationComponent.RenderBounds;
        }

        /*####################################################################*/
        /*                                Events                              */
        /*####################################################################*/

        protected internal override void MouseClick(MouseEventArgs e) {
            if (ClickEvent != null) {
                ClickEvent(this);
            }
        }

        /*####################################################################*/
        /*                              Game Loop                             */
        /*####################################################################*/               

        protected internal override void Update() { }

        protected internal override void Draw() {

            if (IsPressed)
                SpriteBatch.Draw(_pressed, LocationComponent.RenderBounds, Color.White);
            else if (IsHover)
                SpriteBatch.Draw(_hover, LocationComponent.RenderBounds, Color.White);
            else
                SpriteBatch.Draw(_default, LocationComponent.RenderBounds, Color.White);
            
            SpriteBatch.Draw(_text, LocationComponent.RenderLocation, Color.White);
        }        
    }
}